
// Copyright (c) 2010-2023 niXman (github dot nixman at pm dot me). All
// rights reserved.
//
// This file is part of YAS(https://github.com/niXman/yas) project.
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
//
//
// Boost Software License - Version 1.0 - August 17th, 2003
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

#ifndef __yas__tests__base__include__header_hpp
#define __yas__tests__base__include__header_hpp

/***************************************************************************/

template<typename archive_traits>
bool header_test(std::ostream &log, const char* archive_type, const char *test_name) {
    (void)log;
	(void)archive_type;
    (void)test_name;

    {
        using oa = yas::binary_oarchive<yas::mem_ostream>;
        using ia = yas::binary_iarchive<yas::mem_istream>;
        static_assert(oa::type() == yas::binary, "");
        static_assert(__YAS_BIG_ENDIAN ? oa::is_big_endian() : oa::is_little_endian(), "");
        static_assert(oa::is_writable(), "");
        static_assert(!oa::is_readable(), "");

        static_assert(yas::detail::header::k_header_size == oa::header_size(), "");
        static_assert(yas::detail::header::k_header_size == ia::header_size(), "");
    }

    {
        static_assert(yas::binary_oarchive<yas::mem_ostream>::flags() & yas::ehost, "");
        static_assert(yas::text_oarchive<yas::mem_ostream>::flags() & yas::ehost, "");

        static_assert(!(yas::binary_oarchive<yas::mem_ostream, yas::binary|yas::ebig>::flags() & yas::ehost), "");
        static_assert(!(yas::text_oarchive<yas::mem_ostream, yas::text|yas::elittle>::flags() & yas::ehost), "");
    }

    {
        yas::mem_ostream os;
        yas::binary_oarchive<yas::mem_ostream> oa(os);

        yas::mem_istream is(os.get_intrusive_buffer());
        yas::binary_iarchive<yas::mem_istream> ia(is);
    }
    
    {
        yas::vector_ostream<char> os;
        yas::binary_oarchive<yas::vector_ostream<char>> oa(os);

        yas::mem_istream is(os.buf);
        yas::binary_iarchive<yas::mem_istream> ia(is);
    }
    
    {
        yas::vector_ostream<int8_t> os;
        yas::binary_oarchive<yas::vector_ostream<int8_t>> oa(os);

        yas::mem_istream is(os.buf);
        yas::binary_iarchive<yas::mem_istream> ia(is);
    }
    
    {
        yas::vector_ostream<uint8_t> os;
        yas::binary_oarchive<yas::vector_ostream<uint8_t>> oa(os);

        yas::mem_istream is(os.buf);
        yas::binary_iarchive<yas::mem_istream> ia(is);
    }

    {
        __YAS_CONSTEXPR_IF( !yas::is_json_archive<typename archive_traits::oarchive_type>::value ) {
            typename archive_traits::oarchive oa;
            archive_traits::ocreate(oa, archive_type);

            const auto ibuf = oa.get_intrusive_buffer();
            YAS_TEST_REPORT_IF(!yas::is_yas_archive(ibuf), log, archive_type, test_name, return false;);
            const auto sbuf = oa.get_shared_buffer();
            YAS_TEST_REPORT_IF(!yas::is_yas_archive(sbuf), log, archive_type, test_name, return false;);

            const auto header = yas::read_header(ibuf);
            __YAS_CONSTEXPR_IF( yas::is_binary_archive<typename archive_traits::oarchive_type>::value ) {
                YAS_TEST_REPORT_IF(header.bits.version != yas::detail::binary_archive_version, log, archive_type, test_name, return false;);
                YAS_TEST_REPORT_IF(yas::archive_version(ibuf) != yas::detail::binary_archive_version, log, archive_type, test_name, return false;);
                YAS_TEST_REPORT_IF(yas::archive_version(sbuf) != yas::detail::binary_archive_version, log, archive_type, test_name, return false;);
            } else {
                YAS_TEST_REPORT_IF(header.bits.version != yas::detail::text_archive_version, log, archive_type, test_name, return false;);
                YAS_TEST_REPORT_IF(yas::archive_version(ibuf) != yas::detail::text_archive_version, log, archive_type, test_name, return false;);
                YAS_TEST_REPORT_IF(yas::archive_version(sbuf) != yas::detail::text_archive_version, log, archive_type, test_name, return false;);
            }

            const auto artype = archive_traits::oarchive_type::type();
            YAS_TEST_REPORT_IF(yas::archive_type(ibuf) != artype, log, archive_type, test_name, return false;);
            YAS_TEST_REPORT_IF(yas::archive_type(sbuf) != artype, log, archive_type, test_name, return false;);

            __YAS_CONSTEXPR_IF( yas::is_binary_archive<typename archive_traits::oarchive_type>::value ) {
                const auto arendian = (archive_traits::oarchive_type::flags() & (yas::elittle|yas::ebig));
                YAS_TEST_REPORT_IF(yas::archive_endian(ibuf) != arendian, log, archive_type, test_name, return false;);
                YAS_TEST_REPORT_IF(yas::archive_endian(sbuf) != arendian, log, archive_type, test_name, return false;);
            }

            const bool arcompacted = (archive_traits::oarchive_type::flags() & yas::compacted);
            YAS_TEST_REPORT_IF(yas::archive_is_compacted(ibuf) != arcompacted, log, archive_type, test_name, return false;);
            YAS_TEST_REPORT_IF(yas::archive_is_compacted(sbuf) != arcompacted, log, archive_type, test_name, return false;);
        }
    }

    return true;
}

/***************************************************************************/

#endif // __yas__tests__base__include__header_hpp
